/*
 * A RMI Spelling client can check whether the word is spelled correct.
 * Written by YUE CHI
 */

import java.net.SocketException;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.SortedSet;

/**
 * A RMISpelling client to check the word. 
 * @author YUE CHI
 * @version 1.0
 *
 */
public class RMISpellingClient extends AbstractClient
{

  /**
   * The constructor extend the Abstractclient.
   * @param the_args  the input string
   * @throws SocketException if here is some socket problems.
   */
  public RMISpellingClient(final String[] the_args) throws SocketException
  {
    super(the_args);
  }

  /**
   * The main function run the server.
   * @param the_args  the input string from commandline.
   * @throws SocketException if here is some socket problems.
   */
  public static void main(final String[] the_args) throws SocketException
  {
    final RMISpellingClient client = new RMISpellingClient(the_args);
    try
    {
      //Intialize the remotespelling temp.
      final RemoteSpelling temp = (RemoteSpelling) (client.my_registery.lookup(the_args[2]));
      //create a iterator for searching word list.
      final Iterator iterator_word = client.my_searchlist.iterator();
      while (iterator_word.hasNext())
      {
        //get the current word
        final String wordcheck = (String) iterator_word.next();
        //check the word and get the suggestion set.
        final SortedSet<String> tempsuggestions = temp.check(wordcheck);
        Log.out("Querying service for word " + wordcheck);
        
        if (tempsuggestions == null)  // word spell correct.
        {
          Log.out(wordcheck + " is spelled correctly");
        }
        else //incorrect
        {
          //get how many suggestion words.
          final int num_suggestions = tempsuggestions.size();
          //here is suggestions.
          if (num_suggestions > 0)
          {
            final StringBuffer sb = new StringBuffer();
            sb.append(wordcheck + "is spelled incorrctly, there are " + num_suggestions +
                      " suggestions");
            final Iterator iterator_suggestions = tempsuggestions.iterator();
            while (iterator_suggestions.hasNext())
            {
              final String tempsuggestion = (String) iterator_suggestions.next();
              sb.append(' ');
              sb.append(tempsuggestion);
            }
            Log.out(sb.toString());
          }
          else //no suggestions
          {
            Log.out(wordcheck + " is spelled incorrectly, no suggestions");
          }
        }
      }
      return;
    }
    catch (final AccessException e)
    {
      Log.err("Accessing problems " + e);
      e.printStackTrace();
    }
    catch (final RemoteException e)
    {
      Log.err("Error looking up or accessing " + e);
      return;
    }
    catch (final NotBoundException e)
    {
      Log.err("Spelling service cannot bound on registery. Exception occur:  " + e);
      e.printStackTrace();
    }
  }

}
